package com.project.XXXX.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.aldjob.controllerImpl.PtController;
import com.google.gson.Gson;
import com.project.XXXX.controllerInterface.IPtjobMutual;
import com.project.XXXX.inter.tbl_dictMapper;
import com.project.XXXX.inter.tbl_divisionMapper;
import com.project.XXXX.inter.tbl_employerinfoMapper;
import com.project.XXXX.inter.tbl_ptjobMapper;
import com.project.XXXX.inter.tbl_sysuserMapper;
import com.project.XXXX.inter.tbl_workplaceMapper;
import com.project.XXXX.inter.tbl_worktimeMapper;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobForShownWithBlobs;
import com.project.XXXX.tools.ProjectTools;
import com.project.XXXX.inter.tbl_employeeinfoMapper;
import com.project.XXXX.inter.tbl_jobenrollMapper;

@Controller
public class Pt implements IPtjobMutual {
	@Resource
	private tbl_ptjobMapper tbl_ptjobMapper;
	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_worktimeMapper tbl_worktimeMapper;
	@Resource
	private tbl_dictMapper tbl_dictMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	@Resource
	private tbl_divisionMapper tbl_divisionMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_jobenrollMapper tbl_jobenrollMapper;
	@Resource
	private tbl_workplaceMapper tbl_workplaceMapper;

	PtController uc;
	boolean first;

	void prepare(HttpServletResponse response) {
		if (first) {
			// System.out.println("is first");
			uc = new PtController(tbl_ptjobMapper, tbl_dictMapper, tbl_workplaceMapper, tbl_divisionMapper,
					tbl_employerinfoMapper, tbl_jobenrollMapper);
			first = false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	public Pt() {
		first = true;
	}

	@RequestMapping(value = "/api/pt/getCountByTypeandPlace")
	void showByTypeandPlaceCount(@RequestParam("type") String type, @RequestParam("country") String country,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();

		map.put("count", "" + uc.getCountByTypeandPlace(type, country));

		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping(value = "/api/pt/getByTypeandPlace")
	void showByTypeandPlace(@RequestParam("page") int currentPage, @RequestParam("type") String type,
			@RequestParam("country") String country, HttpServletRequest request, HttpServletResponse response,
			Model model, HttpSession session) {
		prepare(response);
		List<PtJobForShown> j = null;
		j = uc.getByTypeandPlace(currentPage, type, country);
		ProjectTools.ResponseOut(response, new Gson().toJson(j));
	}

	@RequestMapping(value = "/api/pt/getDetail")
	public void showItemDetail(@RequestParam("id") int ptid, HttpServletRequest request, HttpServletResponse response,
			Model model, HttpSession session) {
		prepare(response);
		PtJobForShownWithBlobs j = uc.getDetail(ptid);
		String json = null;
		//System.out.println(json);
		if (j == null) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("status", "failure");
			map.put("error_description", "pt ID doesn't exist");
			json = new Gson().toJson(map);
			response.setStatus(403);
		} else {
			json = new Gson().toJson(j);
		}
		ProjectTools.ResponseOut(response, json);
	}

}
